#include "../interpreter.h"
#include "../syntax_tree.h"
#include "../symbol_table.h"
#include "../tiny_y.h"
#include <stdio.h>

symbol_table st=NULL;

int main(int argc, char *argv[])
{
	syntax_t t_const1,t_const2,t_const3,t_true,t_false;
	syntax_t add1,add2,sub1,mul1,div1,if1,if2,if3,if4,id1,assign;
	itpt_ret *ret;
	st=symbol_table_init();
	t_const1=syntax_t_const(123);
	t_const2=syntax_t_const(234);
	t_const3=syntax_t_const(12);
	t_true=syntax_t_const(1);
	t_false=syntax_t_const(0);
	add1=syntax_t_new('+',2,t_const1,t_const2);
	add2=syntax_t_new('+',2,add1,t_const3);
	sub1=syntax_t_new('-',2,t_const2,t_const1);
	mul1=syntax_t_new('*',2,t_const1,t_const3);
	div1=syntax_t_new('/',2,t_const2,t_const3);
	if1=syntax_t_new(IF,2,t_true,div1);
	if2=syntax_t_new(IF,2,t_false,div1);
	if3=syntax_t_new(IF,3,t_true,div1,add2);
	if4=syntax_t_new(IF,3,t_false,div1,add2);
	id1=syntax_t_id("abc");
	assign=syntax_t_new('=',2,id1,t_const1);

	printf ("test const\n");
	syntax_t_print(t_const1);
	ret=interpreter(t_const1);
	printf("t_const1=%d\n",ret->integer);
	itpt_ret_free(ret);
	printf ("+++++++++++++++++++++\n");
	syntax_t_print(t_const2);
	ret=interpreter(t_const2);
	printf("t_const2=%d\n",ret->integer);
	itpt_ret_free(ret);
	printf ("=========================================\n");
	printf ("test add\n");
	syntax_t_print(add1);
	ret=interpreter(add1);
	printf("add1=%d\n",ret->integer);
	itpt_ret_free(ret);
	printf ("+++++++++++++++++++++\n");
	syntax_t_print(add2);
	ret=interpreter(add2);
	printf("add2=%d\n",ret->integer);
	itpt_ret_free(ret);
	printf ("=========================================\n");
	printf ("test sub\n");
	syntax_t_print(sub1);
	ret=interpreter(sub1);
	printf ("sub1=%d\n",ret->integer);
	itpt_ret_free(ret);
	printf ("=========================================\n");
	printf ("test mul\n");
	syntax_t_print(mul1);
	ret=interpreter(mul1);
	printf ("mul1=%d\n",ret->integer);
	itpt_ret_free(ret);
	printf ("=========================================\n");
	printf ("test div\n");
	syntax_t_print(div1);
	ret=interpreter(div1);
	printf ("div1=%d\n",ret->integer);
	itpt_ret_free(ret);
	printf ("=========================================\n");
	printf ("test op if\n");
	syntax_t_print(if1);
	if(ret=interpreter(if1)){
		printf ("if1=%d\n",ret->integer);
		itpt_ret_free(ret);
	}
	printf ("+++++++++++++++++++++\n");
	syntax_t_print(if2);
	if(ret=interpreter(if2)){
		printf ("if2=%d\n",ret->integer);
		itpt_ret_free(ret);
	}
	printf ("+++++++++++++++++++++\n");
	syntax_t_print(if3);
	if(ret=interpreter(if3)){
		printf ("if3=%d\n",ret->integer);
		itpt_ret_free(ret);
	}
	printf ("+++++++++++++++++++++\n");
	syntax_t_print(if4);
	if(ret=interpreter(if4)){
		printf ("if4=%d\n",ret->integer);
		itpt_ret_free(ret);
	}
	printf ("========================================\n");
	printf ("test assignment\n");
	syntax_t_print(assign);
	if(ret=interpreter(assign)){
		printf ("assignment sean succ\n");
		printf ("ret=%d\n",*(int*)ret->var->content);
		itpt_ret_free(ret);
	}
    return 0;
}
